﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Security.Cryptography;

namespace PBO.Data.Util
{
    public static class StringHelper
    {
        public static bool ValidatePmName(string name)
        {
            return Regex.IsMatch(name, @"^\w{1,10}$", RegexOptions.Compiled);
        }

        public static string BW_MSG_REPLACE(string bwpbo_text)
        {
            if (!bwpbo_text.Contains("{")) return bwpbo_text;
            string s = Regex.Replace(bwpbo_text, @"\{[^}]+\}", "咩", RegexOptions.Compiled);
            var array = s.Split('咩');
            s = "";
            for (int i = 0; i < array.Length; i++)
            {
                s += array[i];
                if (i != array.Length - 1)
                {
                    s += "{" + i + "}";
                }
            }
            return s;
        }

        /// <summary>
        /// 字符串转int
        /// </summary>
        public static int ToInt(this string s, int def = 0)
        {
            int i = def;
            int.TryParse(s, out i);
            return i;
            //Match m = Regex.Match(s, @"(?:[+-]|)\d+", RegexOptions.Compiled);
            //return m.Success ? Convert.ToInt32(m.Value) : def;
        }

        /// <summary>
        /// 字符串转long
        /// </summary>
        public static long ToLong(this string s, long def = 0)
        {
            long i = def;
            long.TryParse(s, out i);
            return i;
            //Match m = Regex.Match(s, @"(?:[+-]|)\d+", RegexOptions.Compiled);
            //return m.Success ? Convert.ToInt64(m.Value) : def;
        }

        /// <summary>
        /// 字符串转double
        /// </summary>
        public static double ToDouble(this string s, double def = 0)
        {
            double i = def;
            double.TryParse(s, out i);
            return i;
            //Match m = Regex.Match(s, @"-?(?:\d+(?:\.\d+)?|\.\d+)", RegexOptions.Compiled);
            //return m.Success ? Convert.ToDouble(m.Value) : def;
        }

        public static string SignTopRequest(IDictionary<string, string> parameters, string secret, bool qhs)
        {
            var array = parameters.Where(t => !string.IsNullOrEmpty(t.Key) && !string.IsNullOrEmpty(t.Value)).OrderBy(t => t.Key).Select(t => t.Key + t.Value);
            string query = secret + string.Concat(array);
            if (qhs)
            {
                query += secret;
            }

            MD5 md5 = MD5.Create();
            byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(query));

            StringBuilder result = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                string hex = bytes[i].ToString("X");
                if (hex.Length == 1)
                {
                    result.Append("0");
                }
                result.Append(hex);
            }

            return result.ToString();
        }
    }
}